using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace WindowsGame1
{

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// 

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Texture2D m_texBackGround;
        //Texture2D m_texGround;
        //Camera m_Camera = new Camera();

        //ObjectManager m_ObjectManager = new ObjectManager();

        //KeyboardState m_LastKeyState;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            GlobalContentManager.Instance.SetContent(Content);
            GlobalContentManager.Instance.Graphics = graphics;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferHeight = 640;
            graphics.PreferredBackBufferWidth = 1040;
            graphics.ApplyChanges();


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            StateMachine.Instance.ChangeState(new MainMenuState(this));

            // TODO: use this.Content to load your game content here
            //m_texBackGround = Content.Load<Texture2D>("background");
            //m_texGround = Content.Load<Texture2D>("ground");

            //Player tempPlayer = new Player();
            //tempPlayer.m_Texture = Content.Load<Texture2D>("templatefun");
            //tempPlayer.m_Position = new Vector2(100, 100);
            //m_Camera.m_Target = tempPlayer;
            

            //Equip tempHair = new Equip();
            //tempHair.m_Texture = Content.Load<Texture2D>("Equips/Hair/Hair001");
            //tempPlayer.m_lEquips.Add(tempHair);

            //ObjectManager.m_ObjectManager.m_ObjectList.Add(tempPlayer);

            //Monster tempMonster = new Monster();
            //tempMonster.m_Texture = Content.Load<Texture2D>("rock");
            //ObjectManager.m_ObjectManager.m_ObjectList.Add(tempMonster);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            //Content.Unload();
            StateMachine.Instance.Shutdown();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            float deltaT = (float)(gameTime.ElapsedGameTime.Milliseconds/100.0f);

            // TODO: Add your update logic here
            //ObjectManager.m_ObjectManager.Update(deltaT, m_LastKeyState);
            StateMachine.Instance.Update(deltaT);

            //m_Camera.Update(deltaT);

            //m_LastKeyState = Keyboard.GetState();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            
            //spriteBatch.Draw(m_texGround, new Rectangle((int)((0 - m_Camera.m_Position.X) * m_Camera.m_fZoom), (int)((12 - m_Camera.m_Position.Y) * m_Camera.m_fZoom), (int)(800 * m_Camera.m_fZoom), (int)(600 * m_Camera.m_fZoom)), Color.White);
            //ObjectManager.m_ObjectManager.Render(spriteBatch, m_Camera);
            StateMachine.Instance.Render(spriteBatch);

            base.Draw(gameTime);
        }
    }
}
